package com.feeyin.radar.crm.controller;

import com.feeyin.radar.commons.StringUtils;
import com.feeyin.radar.crm.service.EmployeeManageService;
import com.feeyin.radar.sys.service.DepartmentService;
import com.feeyin.radar.sys.service.UserService;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import org.springframework.web.servlet.view.RedirectView;

public class EmployeeManageController extends MultiActionController
{
  private DepartmentService departmentService;
  private EmployeeManageService employeeManageService;
  private UserService userService;
  private String employeeManage;
  private String employeeList;
  private String viewEmployee;
  private String viewEmployeeLevel;

  public DepartmentService getDepartmentService()
  {
    return this.departmentService;
  }

  public void setDepartmentService(DepartmentService departmentService) {
    this.departmentService = departmentService;
  }

  public EmployeeManageService getEmployeeManageService() {
    return this.employeeManageService;
  }

  public void setEmployeeManageService(EmployeeManageService employeeManageService) {
    this.employeeManageService = employeeManageService;
  }

  public UserService getUserService() {
    return this.userService;
  }

  public void setUserService(UserService userService) {
    this.userService = userService;
  }

  public String getEmployeeManage() {
    return this.employeeManage;
  }

  public void setEmployeeManage(String employeeManage) {
    this.employeeManage = employeeManage;
  }

  public String getEmployeeList() {
    return this.employeeList;
  }

  public void setEmployeeList(String employeeList) {
    this.employeeList = employeeList;
  }

  public String getViewEmployee() {
    return this.viewEmployee;
  }

  public void setViewEmployee(String viewEmployee) {
    this.viewEmployee = viewEmployee;
  }

  public String getViewEmployeeLevel() {
    return this.viewEmployeeLevel;
  }

  public void setViewEmployeeLevel(String viewEmployeeLevel) {
    this.viewEmployeeLevel = viewEmployeeLevel;
  }

  public ModelAndView display(HttpServletRequest request, HttpServletResponse response)
  {
    Map map = new HashMap();
    map.put("departments", this.departmentService.getDepartments());
    return new ModelAndView(getEmployeeManage(), map);
  }

  public ModelAndView preEmployeeList(HttpServletRequest request, HttpServletResponse response)
  {
    Map map = new HashMap();

    Integer currentUserId = (Integer)request.getSession().getAttribute("userId");
    String deptId = request.getParameter("deptId");
    map.put("deptId", deptId);
    if (!StringUtils.isEmpty(deptId))
    {
      map.put("users", this.employeeManageService.getEmployeesByDeptIds(Integer.valueOf(Integer.parseInt(deptId)), currentUserId));
    }
    return new ModelAndView(getEmployeeList(), map);
  }

  public ModelAndView updateEmployeeLevel(HttpServletRequest request, HttpServletResponse response)
  {
    Map map = new HashMap();

    String deptId = request.getParameter("deptId");
    map.put("deptId", deptId);
    String userId = request.getParameter("id");
    String sign = request.getParameter("sign");

    if ((!StringUtils.isEmpty(userId)) && 
      (!StringUtils.isEmpty(sign))) {
      this.employeeManageService.updateEmployeeLevel(Integer.valueOf(Integer.parseInt(userId)), Integer.parseInt(sign));
    }
    return new ModelAndView(new RedirectView("./employeeManage.do?action=preEmployeeList&deptId=" + deptId));
  }

  public ModelAndView viewEmployee(HttpServletRequest request, HttpServletResponse response)
  {
    Map map = new HashMap();

    String id = request.getParameter("id");
    if (!StringUtils.isEmpty(id)) {
      map.put("user", this.userService.getUser(Integer.valueOf(Integer.parseInt(id))));
    }
    return new ModelAndView(getViewEmployee(), map);
  }

  public ModelAndView viewEmployeeLevel(HttpServletRequest request, HttpServletResponse response)
  {
    Map model = new HashMap();
    model.put("departments", this.employeeManageService.getEmployeeLevels());
    return new ModelAndView(getViewEmployeeLevel(), model);
  }

  public void uploadExportFile(HttpServletRequest request, HttpServletResponse response)
  {
    Integer userId = (Integer)request.getSession().getAttribute("userId");
    String realPath = getServletContext().getRealPath("/");

    boolean result = false;
    result = this.employeeManageService.saveWaitExportFile(realPath, userId);

    if (result) {
      String basePath = realPath + "upload" + System.getProperty("file.separator") + "temp";
      String path = basePath + System.getProperty("file.separator") + "employeeLevel" + userId + ".xls";
      try
      {
        File file = new File(path);

        String filename = file.getName();

        String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();

        InputStream fis = new BufferedInputStream(new FileInputStream(path));
        byte[] buffer = new byte[fis.available()];
        fis.read(buffer);
        fis.close();

        response.reset();

        response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
        response.addHeader("Content-Length", file.length()+"");
        OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
        response.setContentType("application/octet-stream");
        toClient.write(buffer);
        toClient.flush();
        toClient.close();
      } catch (IOException ex) {
        ex.printStackTrace();
      }
    }
  }
}